/**
 * 2016年12月23日
 * 百人牛牛房间
 * 添加roomManager, 使用全局变量保存room 的实例，方便调用
 * 也可以在这里添加一个全局变量保存room实例
 */
var pomelo = require('pomelo');

const wt = require('async').waterfall;

var PlayerManager = require('./playerManager');
var consts = require('./consts');
var round = require('./round');

var client = pomelo.app.get('redisUser');
var mysql = require('../../dao/userDao');

var log = console.log;

var logger = require('pomelo-logger').getLogger('room', __filename, process.pid).info;

function Room(gameId, roomId) {

    console.log("create room:");
    logger('create Room!');
    var self = this;
    self.freeTime = 1;// 空闲时长
    self.xiaZhuTime = 3;// 下注时长
    self.kaiPaiTime = 5;// 开牌时长
    self.checkTime = consts.GAME.TIME.UNKNOWN;// 倒计时

    self.gameStatus = consts.GAME.STATUS.UNKNOWN;
    self.hInterval = null;// 定时器

    self.tempBidData = {};  // 临时存储一个倒计时广播内的下注情况

    self.playerManager = new PlayerManager(self);

    self.cardPool = [];   // 牌池

    self.curCards = null;
    self.curResultOdds = {};

    self.curBanker = 0;  // 记录当前庄家的 playerId
    self.curBankerRound = 0;  // 记录当前庄家参与局数，坐庄满20局就下庄
    self.curBankerScore = 0;  // 记录庄的分数，也就是赚的钱数
    self.applyBankerList = [];  // 申请上庄玩家列表
    self.bankerOnLine = true;  // 记录庄家是否掉线了

    self.cutInBanker = 0;

    self.offlinePlayer = [];  // 掉线的玩家列表


    self.channel = null;
    self.roomId = roomId;
    self.gameId = gameId;
    self.name = 'room' + roomId;
    self.channelService = pomelo.app.get('channelService');
    self.createChannel();
    self.initGame();
}

var pro = Room.prototype;

/**
 *  创建房间channel
 */
Room.prototype.createChannel = function () {
    if (this.channel) {
        return this.channel;
    }
    var channelName = this.gameId;
    this.channel = this.channelService.getChannel(channelName, true);

    return this.channel;
};


/**
 * 房间初始化
 * 定时器开启，牌池初始化，开始运转牌局
 */
pro.initGame = function () {
    var self = this;

    self.SetGameStatus(consts.GAME.STATUS.FREE, 1); // 最开始有1秒空闲时间
    console.debug('game begin =============================change gameStatus free: ', self.checkTime);
    if (!self.hInterval) {
        // 第三个参数作为self.OnTimer这个函数的参数传入
        self.hInterval = setInterval(self.OnTimer, 1000, self);
    }
};

/**
 * 设置游戏状态
 */
Room.prototype.SetGameStatus = function (status, time) {
    var self = this;
    self.gameStatus = status;
    self.checkTime = time;
};

/**
 * 获取游戏状态
 */
Room.prototype.getGameStatus = function () {
    var self = this;
    var retDict = {
        gameStatus: self.gameStatus,
        checkTime: self.checkTime
    };
    return retDict;
};

/**
 * 时间运转
 */
pro.OnTimer = function (ptr) {
    var self = ptr;

    if (self.checkGameStatus('free')) { // 空闲时间
        self.ProcessCheckFree();
    }
    else if (self.checkGameStatus('xiazhu')) { // 下注时间
        self.ProcessCheckXiaZhu();
    }
    else if (self.checkGameStatus('kaipai')) { // 开牌时间
        self.ProcessCheckKaiPai();
    }
    console.log(self.getGameStatus());
};

/**
 * 空闲时间
 */
Room.prototype.ProcessCheckFree = function () {
    var self = this;

    // 倒计时，广播时间
    self.checkTime--;

    if (self.checkTime <= 0) {
        // 空闲倒计时结束，开始下注
        self.SetGameStatus(consts.GAME.STATUS.XIAZHU, self.xiaZhuTime);

        // 初始化游戏信息
        // 更新到下注状态之前，初始化下注信息
        self.bidCount = 0;
        self.playerManager.bidData = {};

        var ret = self.getGameStatus();
        self.channel.pushMessage('onGameXiaZhu', ret, null);
        self.SetGameStart(); // 游戏开始之后才可以下注

        log('====================================change gameStatus xiaZhu: ', ret);
    }
};

/**
 * 下注时间
 */
Room.prototype.ProcessCheckXiaZhu = function () {
    var self = this;

    self.ProcessXiaZhu();  // 广播下注信息
    self.checkTime--;

    if (self.checkTime == 0) {  // 转为开牌  // 把<=0改为==0，避免产生结算两次的可能
        // 只用来计算
        console.log('self.getGameStatus()     >>>>>>>>>>>>>>');
        console.log(self.getGameStatus());

        self.SetGameEnd(function () {
            // 这样就可以确定只在checkTime = 0 的时候执行一次
            console.log('in setGameEnd cb! >>>>>>>>>>>>>>>>>>');
            console.log('结算及广播完成');
        });
    }

    if (self.checkTime <= 0) {
        // 等于0时就已经转换到下一个状态，转换状态和结算分离开
        self.SetGameStatus(consts.GAME.STATUS.KAIPAI, self.kaiPaiTime);
        var ret = self.getGameStatus();
        self.channel.pushMessage('onGameKaiPai', ret, null);
        console.debug('======================================change gameStatus kaiPai: ', ret);
    }
};

/**
 * 开牌时间
 */
Room.prototype.ProcessCheckKaiPai = function () {
    var self = this;
    self.checkTime--;

    if (self.checkTime <= 0) {
        // 广播结算信息之后就清空,防止出现下一轮仍旧下注的情况
        self.SetGameStatus(consts.GAME.STATUS.FREE, self.freeTime);

        var ret = self.getGameStatus();
        self.channel.pushMessage('onGameFree', ret, null);
        log('==========================================change gameStatus free: ', ret);
    }
};

/**
 * 开始游戏,运行牌局
 */
Room.prototype.SetGameStart = function () {
    var self = this;

    var ret = self.getGameStatus();
    self.channel.pushMessage('onGameStart', ret, null);

    self.resetConfig--;
    // 游戏开始之前读取redis配置
    self.loadGameConfig(function (err) {
        if (err) {
            log(err);
            throw new Error(err);
        }
        self.getCardResult();
    });
};

/**
 * 从redis中读取游戏配置
 */
Room.prototype.loadGameConfig = function (cb) {
    log('Room 读取redis游戏配置');

    var self = this;
    var gameId = self.gameId;

    // if (self.loadedConfig && self.resetConfig >= 0) {
    if (self.resetConfig >= 0) {
        log('未到重新设置时间');
        cb();
        return;
    }

    var key = 'gameConfig' + self.gameId;
    var config, redisCache = true;

    wt([
        function (wcb) {
            log('redis 查询配置');
            client.get(key, function (err, rel) {
                if (err) {
                    return wcb(err);
                }
                if (rel) {
                    config = JSON.parse(rel);
                }
                else {
                    redisCache = false;
                }

                wcb(null);
            });
        },
        function (wcb) {

            if (redisCache) {
                return wcb(null);
            }
            log('mysql 查询配置');

            var sql = 'select id, configs from game_info where id = ?';
            var args = [gameId];
            mysql.query(sql, args, function (err, rows) {
                if (err) {
                    return wcb(err);
                }

                if (rows[0].configs) {
                    config = JSON.parse(rows[0].configs);
                }
                else {
                    config = { "id": gameId };
                }
                wcb(null);
            });
        },
        function (wcb) {
            if (redisCache) {
                return wcb(null);
            }
            log('redis 写入配置');

            client.set(key, JSON.stringify(config), function (err, rel) {
                if (err) {
                    wcb(err);
                }
                else {
                    wcb(null);
                }
            });
        }
    ],
        function (err, rel) {
            if (!err) {
                self.config = config;
                self.resetRoomConfig();
                return cb(null);
            }
            else {
                return cb(err);
            }
        });
};

/**
 * 设置房间配置
 */
Room.prototype.resetRoomConfig = function () {
    log('设置房间配置');
    var self = this;

    if (self.config) {
        self.freeTime = self.config.FREE_TIME || self.freeTime;
        self.xiaZhuTime = self.config.XIAZHU_TIME || self.xiaZhuTime;
        self.kaiPaiTime = self.config.KAIPAI_TIME || self.kaiPaiTime;

        self.resetConfig = parseInt(self.config.RESET_CONFIG) || self.resetConfig;

        if (self.config.ROBOT) {
            var robot = self.config.ROBOT;
        }

        if (parseInt(self.config.TAX) > -1) {
            // log('已配置税率：', room.config.TAX);
            self.tax = parseInt(self.config.TAX);
        }
        else {
            self.tax = consts.ROOM.TAX;
        }
    }

};

// 查看配置，获取牌的设置
Room.prototype.getCardResult = function () {
    console.log('getCardResult.............');

    var self = this;

    var key = 'gameCards' + this.gameId;

    client.lrange(key, 0, -1, function (err, rel) {
        if (err) {
            console.log(err);
            console.warn('redis错误！');
        }
        else {
            if (!isNaN(parseInt(rel))) {
                log(rel);
                rel = parseInt(rel);
                round.replaceNum(rel);
            }
        }
        self.RunCardRound();
    });
};

/**
 * 运行牌局，记录牌局结果，记录赔率
 */
Room.prototype.RunCardRound = function () {
    var self = this;

    var result = round.next(self.roomId);

    log('本局结果', result);

    self.curResult = result.ret;
    self.curResultOdds = result.odds;
};

/**
 * 定时通知前端，其他人的下注情况
 */
Room.prototype.ProcessXiaZhu = function () {
    console.log('Room.ProcessXiaZhu caught >>>>>>>>>>>>>>>>>>>>>>>');
    var self = this;

    var ret = self.tempBidData;
    var count = 0;
    for (var k in ret) {
        if (ret.hasOwnProperty(k)) {
            count++;
            break;
        }
    }

    if (count) {  // 只有有人下注才会发广播
        self.channel.pushMessage('onGameBid', ret, null);
    }

    self.tempBidData = {};  // 通知之后就清空临时变量

};

/**
 * 下注处理
 */
Room.prototype.bid = function (playerId, msg) {
    console.log('Room.bid caught >>>>>>>>>>>>>>>>>>>>>>');
    var self = this;
    var bidData = this.playerManager.bidData;
    var tempBidData = this.tempBidData;

    var odds = {
        "1": 40,
        "2": 30,
        "3": 20,
        "4": 10,
        "5": 5,
        "6": 5,
        "7": 5,
        "8": 5
    };

    if (!tempBidData[playerId]) {
        tempBidData[playerId] = {};
    }

    // 记录下注信息，扣除玩家金钱

    for (var k in msg) {

        // 是否已经下注
        if (!bidData[playerId]) {
            bidData[playerId] = {};
        }
        // 是否已经在该位置下注
        if (!bidData[playerId][k]) {
            bidData[playerId][k] = 0;
        }

        // if (!bidData[k][playerId]) {
        //     bidData[k][playerId] = 0;
        // }

        self.bidCount += (msg[k] * odds[k]);
        // 检查下注是否可行
        if (!this.checkBidCount()) {
            return false;
        }
        bidData[playerId][k] += msg[k];
        self.playerManager.playerData[playerId].coin -= msg[k];
    }

    // 1.记录下注信息
    // 2.扣除玩家金钱

    var jetton;  // jetton 筹码
    for (k in msg) {
        jetton = msg[k];
        if (!tempBidData[playerId][k]) {
            tempBidData[playerId][k] = {};
            tempBidData[playerId][k][jetton] = 0;
        }
        tempBidData[playerId][k][jetton]++;
    }

    log(bidData);
    log(tempBidData);

    return true;
};

/**
 * 检查下注是否可行
 */
pro.checkBidCount = function () {

    var curBanker;
    if (this.curBanker) {
        curBanker = this.curBanker;
    }
    else {
        return true;
    }
    var bankerCoin = this.playerManager.playerData[curBanker].coin;
    var bidCount = this.bidCount;
    if (bidCount >= bankerCoin) {
        return false;
    }
    return true;
};

/**
 * 下注时间截止，结算
 */
Room.prototype.SetGameEnd = function (cb) {
    console.log('before gameEnd callback! >>>>>>>>>>>>>>>>');
    var self = this;

    if (this.existsBanker()) {
        this.curBankerRound++;
    }

    var ret = self.curResult;
    self.channel.pushMessage('onGameOver', { ret }, null);

    self.CalcResult(self.curResultOdds, cb);

};

/**
 * 结算, 广播结算信息
 * 广播此时的用户列表
 */
Room.prototype.CalcResult = function (resultOdds, cb) {
    var self = this;
    console.log('Room.CalcResult caught >>>>>>>>>>>>>>>>>');
    console.log(resultOdds);

    self.playerManager.calcResultOnly(resultOdds, function (err, ret) {
        if (!err) {
            log('room 结算结束！', ret);
            self.channel.pushMessage('onCalcResult', { data: ret }, null);

            cb();

            // 检查庄的局数是否已经超过
            self.checkBanker();

            // 检查是否有掉线玩家
            self.checkPlayer();
        }
        else {
            console.log(err);
            throw new Error('calcResult Error in room.js, err:', err);
        }
    });

};


/**
 * 2016年12月26日
 * 进入房间
 * in: msg: {serverId, playerId, token, gameId, playerName, isRobot, ip}
 */
pro.addPlayer = function (data, cb) {
    var self = this;

    if (!data || typeof data !== 'object') {
        cb('登录信息有误！', false);
        return;
    }

    this.playerManager.addPlayer(data, function (err) {
        console.log(err);
        if (err && !data.isRobot) {
            cb(err);
            return;
        }

        if (!self.playerManager.isPlayerInRoom(data.playerId)) {
            cb('未能成功将玩家:' + data.playerId + ' 加入房间 ' + this.roomId);
            return;
        }

        // 加入 channel并且广播
        if (!self.addPlayer2Channel(data)) {
            cb('未能成功将玩家:' + data.playerId + ' 加入频道！');
            return;
        }

        cb(null, self.roomId);
    });
};

/**
 * 用户退出房间
 */
pro.kickPlayer = function (data) {
    console.log('Room.kickPlayer caught >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
    // console.log(data);

    // 判断数据格式
    if (!data || typeof data !== 'object') {
        return consts.ROOM.SYS_ERROR;
    }

    // 判断是否在上庄列表，如果在的话就去掉
    var index = this.applyBankerList.indexOf(data.playerId);
    if (index > -1) {
        this.applyBankerList.splice(index, 1);
        var cancelBanker = { bankerId: data.playerId };
        this.channel.pushMessage('onCancelBanker', cancelBanker, null);
    }

    // 判断是否是庄家
    if (this.curBanker == data.playerId) {
        if (this.checkGameStatus('xiazhu')) {
            // 庄家下注时候掉线，要等到这一局结算之后才能踢掉
            this.bankerOnLine = false;  // 庄在线标识设为false
            return true;  // 不能直接踢出房间，要等到结算之后才可以
        }
        else {
            // 庄家非下注时间掉线，直接换庄
            this.setBanker();
        }
    }

    // 判断是否在这个房间里
    if (!this.playerManager.isPlayerInRoom(data.playerId)) {
        return consts.ROOM.LEAVE_ROOM_RET_CODE.NOT_IN_ROOM;
    }

    // 判断是否成功踢出内存
    if (!this.playerManager.kickPlayer(data)) {
        return consts.SYS_ERROR;
    }

    // 不管如何，只要掉线，就要踢出频道
    // 判断是否成功踢出channel
    if (!this.kickPlayer2Channel(data)) {
        return consts.SYS_ERROR;
    }

    // 判断是否还在房间并且不在下线玩家列表
    if (this.playerManager.isPlayerInRoom(data.playerId) && !(this.offlinePlayer.indexOf(data.playerId) > -1)) {
        return consts.SYS_ERROR;
    }

    return consts.ROOM.LEAVE_ROOM_RET_CODE.OK;
};

/**
 * 用户加入房间频道
 */
pro.addPlayer2Channel = function (data) {
    console.log('Room.addPlayer2Channel caught >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
    if (!this.channel) {
        return false;
    }

    if (data) {
        var uid = data.playerId;
        var sid = data.serverId;
        this.channel.add(uid, sid);

        var ret = this.playerManager.getPlayerData(data.playerId);

        ret.playerId = data.playerId;
        this.channel.pushMessage('onPlayerEnter', ret, null);
        // playerLogger('enter', ret);

        return true;
    }
    return false;
};

/**
 * 用户退出房间频道
 */
Room.prototype.kickPlayer2Channel = function (data) {

    var self = this;

    if (!this.channel) {
        return false;
    }

    if (data) {
        self.channel.leave(data.playerId, data.serverId);
        // 广播用户下线
        self.channel.pushMessage('onPlayerLeave', { playerId: data.playerId }, null);
        // playerLogger('leave', { playerId: data.playerId });
        return true;
    }
    return false;
};

/**
 * 获取房间的用户数量
 */
Room.prototype.getPlayerNum = function () {
    return this.playerManager.getPlayerNum();
};

// 获取用户列表
Room.prototype.getPlayerList = function () {
    return this.playerManager.getPlayerList();
};

// 获取玩家状态
Room.prototype.getPlayerData = function (playerId) {
    // var self = this;
    // var ret = self.playerManager.getPlayerData(playerId);
    // return ret;
    return this.playerManager.getPlayerData(playerId);
};

// 检查金钱数是否足够坐庄
Room.prototype.checkApplyBanker = function (playerId) {
    var players = this.playerManager.playerData;
    var coin = players[playerId].coin;
    return coin > consts.ROOM.MIN_BANKER_COIN;
};

// 申请上庄
// 如果只有一个人，立即上庄
Room.prototype.applyBanker = function (msg, cb) {
    var playerId = msg.playerId;
    var ret = null;
    var applyBanker = {};
    if (this.checkApplyBanker(playerId)) {  // 检查是否有资格上庄
        this.applyBankerList.push(playerId);

        // 广播上庄成功消息
        applyBanker = { bankerId: playerId };
        this.channel.pushMessage('onApplyBanker', applyBanker, null);

        // bankerLog.log('apply', applyBanker);

        // 如果只有一个申请上庄的且当前没有庄，就重新设置庄
        if (this.applyBankerList.length === 1 && this.curBanker === 0) {
            this.setBanker();
        }
        cb(ret, true);
    }
    else {
        ret = { err: '金钱不足！' };
        cb(ret, false);
    }
};

// 申请下庄
// 如果是当前的庄要下庄，需要在空闲或者开牌阶段才能下
// 如果是等待着的要下庄，可以立即下庄
// 先换庄，才广播下庄信息
Room.prototype.cancelBanker = function (msg, cb) {
    var playerId = msg.playerId;
    var index = 0;
    var err = null;
    var cancelBanker = {};
    var needSetBanker = false;
    // 判断是不是当前的庄
    if (playerId === this.curBanker) {
        if (this.checkGameStatus('xiazhu')) {
            err = '下注时间不能下庄！';
        }
        else {
            // 如果可以下庄，就需要换庄
            needSetBanker = true;
        }
    }
    else {
        index = this.applyBankerList.indexOf(playerId);
        if (index > -1) {
            this.applyBankerList.splice(index, 1);
        }
        else {
            err = '上庄列表中没有此用户 (id: ' + playerId;
        }
    }

    if (err) {
        cb({ err }, false);
    }
    else {
        cb(null, true);
        // 广播下庄成功消息
        cancelBanker = { bankerId: playerId };
        this.channel.pushMessage('onCancelBanker', cancelBanker, null);

        // bankerLog.log('cancel', cancelBanker);

        // 如果需要换庄，就换庄; 从列表中退出不需要换庄
        // 要下庄成功之后再换庄
        if (needSetBanker) {
            this.setBanker();
        }
    }
};

// 申请插队上庄
Room.prototype.setBanker = function (msg, cb) {
    var playerId = msg.playerId;
    var coin = this.playerManager[playerId].coin;

    var idx = 0;
    var err = null;

    // 既有插队的，插队的又和当前庄家不同 说明上一个插队的还没有上庄，暂时还不能插他前面
    if (this.cutInBanker && this.cutInBanker !== this.curBanker) {
        err = '已经有人插队了！';  // 已经有人插队了
    }

    if (!err && coin > consts.ROOM.MIN_BANKER_COIN + consts.ROOM.CUT_IN_BANKER_COIN) {
        // 如果钱够就先扣钱
        this.playerManager[playerId].coin -= consts.ROOM.CUT_IN_BANKER_COIN;
        // 移动此用户到队头
        idx = this.applyBankerList.indexOf(playerId);
        if (idx > 0) {
            // 如果在队里且不在队头，就先移出队伍
            this.applyBankerList.splice(idx, 1);
        }
        this.applyBankerList.unshift(playerId); // 插入队伍。这种情况下没有上庄也可以插队

        this.cutInBanker = playerId;  // 设置插队用户ID

        // 广播插队成功消息
        var ret = { bankerList: this.applyBankerList };
        this.channel.pushMessage('onCutInBanker', ret, null);

        // bankerLog.log('cutIn', playerId);
    }
    else {
        err = 'money not enough!';
    }

    if (err) {
        cb({ rel: err });
    }
    else {
        cb({ rel: 'ok!' });
    }
};

// 换庄
// 换庄类型： 0.从列表中退出，1.主动下庄，2.局数够了下庄，3.钱数不够下庄，4.掉线下庄
Room.prototype.setBanker = function () {
    var setBanker = {};
    var self = this;
    if (this.applyBankerList.length === 0) {
        self.setBankerData(false);
    }
    else {
        // 换庄，初始化局数和分数
        self.setBankerData(true);

        if (this.cutInBanker && this.cutInBanker == this.curBanker) {
            this.cutInBanker = 0;  // 设置插队玩家为庄家之后，就要初始化插队玩家ID记录
        }

        // 广播换庄成功消息
        setBanker = { bankerId: this.curBanker };
        this.channel.pushMessage('onSetBanker', setBanker, null);
    }

    // bankerLog.log('set', setBanker);
    logger('setBanker', setBanker);

    this.bankerOnLine = true;  // 换庄完成之后要恢复在线状态
    // 不管是否有新庄，都要改为在线状态  // 其实应该是bankerOffline, 庄掉线

    return true;
};

// 设置换庄信息
// opt === true : 有新庄 否则无新庄
Room.prototype.setBankerData = function (opt) {
    var playerId = 0;
    var noBanker = true;
    if (opt) {
        // 循环直到找到能上庄的，或者直到申请列表为空
        // 防止出现换到的庄不够上庄金额的情况
        while (noBanker) {
            if (this.applyBankerList.length === 0) {
                this.curBanker = 0;
                noBanker = false;
                break;
            }
            // else {
            playerId = this.applyBankerList.shift();  // 检查此人是否符合金钱要求
            if (this.checkApplyBanker(playerId)) {
                this.curBanker = playerId;
                noBanker = false;
            }
            // }
        }
    }
    else {
        this.curBanker = 0;
    }
    this.curBankerRound = 0;
    this.curBankerScore = 0;
};

// 检查庄的局数
// 检查庄是否在线
Room.prototype.checkBanker = function () {
    var roundCount = this.getCurBankerRound();
    var needSetBanker = false;
    var needKickBanker = false;

    if (roundCount >= consts.ROOM.MAX_BANKER_ROUND) {
        needKickBanker = true;
    }

    // 如果有庄且庄的金钱少了
    if (this.curBanker && this.getCurBankerCoin() < consts.ROOM.MIN_BANKER_COIN) {
        needKickBanker = true;
    }

    if (needKickBanker) {
        // 换庄前要广播
        var cancelBanker = { bankerId: this.curBanker };
        this.channel.pushMessage('onCancelBanker', cancelBanker, null);

        // bankerLog.log('cancel', cancelBanker);

        this.setBanker();
    }

    // 如果结算时这个庄家掉线了
    if (!this.bankerOnLine) {
        this.setBanker();
    }
};

// 检查玩家是否在线
Room.prototype.checkPlayer = function () {
    var offlinePlayer = this.offlinePlayer;
    var len = offlinePlayer.length;
    var players = this.playerManager.playerData;
    if (len <= 0) {
        return;
    }

    for (var i = 0; i < len; i++) {
        // 之前已经通知过下线，这里不用再通知
        delete players[offlinePlayer[i]];

        this.playerNum--;
    }
};

// 是否有庄
Room.prototype.existsBanker = function () {
    return this.getCurBanker() > 0;
};

// 获取到庄的id
Room.prototype.getCurBanker = function () {
    return this.curBanker;
};

// 获取到庄的金钱
Room.prototype.getCurBankerCoin = function () {
    if (this.curBanker) {
        return this.playerManager.playerData[this.curBanker].coin;
    }
    else {
        return 0;
    }
};

// 获取到庄的局数
Room.prototype.getCurBankerRound = function () {
    return this.curBankerRound;
};

// 获取庄的分数
Room.prototype.getCurBankerScore = function () {
    return this.curBankerScore;
};

// 获取到庄的信息
Room.prototype.getCurBankerData = function () {
    return {
        bankerId: this.getCurBanker(),
        bankerRound: this.getCurBankerRound(),
        bankerScore: this.getCurBankerScore()
    };
};

// 获取到庄的列表
Room.prototype.getBankerList = function () {
    return this.applyBankerList || [];
};

/**
 *检查游戏状态
 */
Room.prototype.checkGameStatus = function (type) {
    var gameStatus = this.getGameStatus().gameStatus;
    var constConf = consts.GAME.STATUS;

    switch (type) {
        case 'free': return gameStatus === constConf.FREE;
        case 'xiazhu': return gameStatus === constConf.XIAZHU;
        case 'kaipai': return gameStatus === constConf.KAIPAI;
        default: return 0;
    }
};


module.exports = Room;
